/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Order;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.MemberDao;
import com.hboxs.asl.dao.SnDao;
import com.hboxs.asl.dao.TradeDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Sn;
import com.hboxs.asl.entity.Trade;
import com.hboxs.asl.service.GlobalConfigService;
import com.hboxs.asl.service.TradeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Service - 充值
 */
@Service("tradeServiceImpl")
public class TradeServiceImpl extends BaseServiceImpl<Trade, Long> implements TradeService {

    @Resource(name = "tradeDaoImpl")
    private TradeDao tradeDao;
    @Resource(name = "snDaoImpl")
    private SnDao snDao;
    @Resource(name = "memberDaoImpl")
    private MemberDao memberDao;
    @Resource(name = "globalConfigServiceImpl")
    private GlobalConfigService globalConfigService;

    @Resource(name = "tradeDaoImpl")
    public void setBaseDao(TradeDao tradeDao) {
        super.setBaseDao(tradeDao);
    }

    @Transactional(readOnly = true)
    public Trade findByTn(String tn) {
        return tradeDao.findByTn(tn);
    }

    @Override
    public Trade create(Member member, BigDecimal amount, Trade.Style style) {
        Trade trade = new Trade();
        trade.setTn(snDao.generate(Sn.Type.charge));
        trade.setAmount(amount);
        trade.setMember(member);
        trade.setStyle(style);
        trade.setTradeStatus(Trade.TradeStatus.create);
        //设置比例
        trade.setPercentage(globalConfigService.getUniqueness().getPercentage());
        tradeDao.merge(trade);
        return trade;
    }

    @Override
    public Trade createTrade(Member member, BigDecimal amount, Trade.Style style, BigDecimal prePoint) {
        Trade trade = new Trade();
        trade.setTn(snDao.generate(Sn.Type.charge));
        trade.setAmount(amount);
        trade.setMember(member);
        trade.setStyle(style);
        trade.setTradeStatus(Trade.TradeStatus.create);
        //设置比例
        trade.setPercentage(globalConfigService.getUniqueness().getPercentage());
        //充值前积分
        trade.setPrePoint(prePoint);
        //充值后积分
        trade.setAfterPoint(member.getPoint());
        trade.setCompleteDate(new Date());
        trade.setTradeStatus(Trade.TradeStatus.done);
        tradeDao.merge(trade);
        return trade;
    }

    @Override
    public void completeTrade(Trade trade) {
        Member member = trade.getMember();
        //充值前积分
        trade.setPrePoint(member.getPoint());
        member.setPoint(member.getPoint().add(trade.getAmount().divide(globalConfigService.getUniqueness().getPercentage())));
//        member.setBalance(member.getBalance().add(trade.getAmount()));
        memberDao.merge(member);
        //充值后积分
        trade.setAfterPoint(member.getPoint());
        trade.setCompleteDate(new Date());
        trade.setTradeStatus(Trade.TradeStatus.done);
        super.update(trade);
    }

    @Override
    public Page findPage(Pageable pageable, Member member, Trade.Style style) {
        pageable.getFilters().add(Filter.eq("style", style));
        pageable.getFilters().add(Filter.eq("member", member));
        pageable.getOrders().add(Order.desc("completeDate"));
        return findPage(pageable);
    }

    @Override
    public Page findPageSuccess(Pageable pageable, Member member) {
        return tradeDao.findPageSuccess(pageable, member);
    }

    @Override
    public Page findPageSuccess(Pageable pageable) {
        return tradeDao.findPageSuccess(pageable);
    }

    @Override
    public Trade createRechargeTrade(Member member, BigDecimal amount) {
        Trade trade = new Trade();
        trade.setTn(snDao.generate(Sn.Type.charge));
        trade.setAmount(amount);
        trade.setMember(member);
        trade.setStyle(Trade.Style.pay);
        trade.setCompleteDate(new Date());
        trade.setTradeStatus(Trade.TradeStatus.done);
        tradeDao.persist(trade);
        return trade;
    }
}